package com.gong.suanfa.binarysearch;

/**
 * @author GongChang
 * @version 1.0
 * @date 2021/6/11 0011 9:19
 */
public class BinarySearch {

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        int search = search(arr, 7, 0, arr.length - 1);

        System.out.println(search);
    }

    public static int search(int[] arr,int target,int left,int right){
        if (left >= right){
            if (arr[left] == target){
                return left;
            }else {
                return -1;
            }
        }
        int mid = left+(right-left)/2;
        if (arr[mid] == target){
            return mid;
        }else if (arr[mid] > target){
            return search(arr,target,left,mid-1);
        }else {
            return search(arr,target,mid+1,right);
        }
    }
}
